// 引入AES.js
var baseJs = require("../../common/base.js");
Page({
  data: {
    uid: wx.getStorageSync('uid'),
    backImg: [],//获得的背景图
    showtext:false,//图片加载完毕显示勾选框等
    checked: true,//会员协议的勾选
    CanHelpBuyAndComePersonEdu: '',//剩余可收现额度
    DepositEdu: '',//押金的金额
    showPayPwdInput: false,//支付密码的输入框
    payFocus: true,//输入密码的聚焦
    payPasswords: '',//输入的密码
    IsHavePayDeposit: '',//是否支付押金
    isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),//是否设置支付密码
  },

  onLoad() {
    this.GetBusinessData()
  },
  // 取得图片数据
  GetBusinessData() {
    var uid = this.data.uid
    wx.request({
      url: baseJs.api + baseJs.encryUrl('method=GetBusinessData&UID=' + uid),
      success: res => {
        var json = JSON.parse(baseJs.decryUrl(res.data))
        var backImg = this.data.backImg
        console.log(json);
        backImg.push(json.ResultData.Img1)
        backImg.push(json.ResultData.Img2)
        backImg.push(json.ResultData.Img3)
        this.setData({
          backImg: backImg,
          CanHelpBuyAndComePersonEdu: json.ResultData.CanHelpBuyAndComePersonEdu,
          DepositEdu: json.ResultData.DepositEdu,
          IsHavePayDeposit: json.ResultData.IsHavePayDeposit
        })
      }
    })
  },
  // 图片加载完成
  imgload() { 
    this.setData({
      showtext:true
    })
  },
  // 点击会员协议跳转详细页面
  huiyuanxieyi() {
    wx.navigateTo({
      url: '/packageB/vipagreement/vipagreement',
    })
  },
  // 会员协议的勾选
  checkboxChange() {
    this.setData({
      checked: !this.data.checked
    })
  },
  // 支付或退还押金
  PayDeposit(e) {
    if (e.currentTarget.dataset.index == 1) { //点击第二张图片
      if (this.data.checked) { //判断是否勾选协议
        this.showPayPwdInput()//弹出输入密码框
      } else {
        wx.showToast({
          title: '请先勾选会员协议',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
  // 显示密码的弹框
  showPayPwdInput() {
    if (this.data.isAlreadyPayPassword) {//判断是否设置支付密码
      this.setData({
        showPayPwdInput: true
      })
    } else {
      wx.showToast({
        title: '请先设置支付密码',
        icon: 'none',
        duration: 2000
      })

    }

  },
  //输入密码,监听
  inputPwd(e) {
    console.log(e);
    this.setData({
      payPasswords: e.detail.value
    });
  },
  // 密码框的取消
  cancelInputPasswords() {
    this.setData({
      showPayPwdInput: false,
      payPasswords: ''
    })
  },
  // 输入密码的确定
  surePasswords() {
    var uid = this.data.uid
    var payPasswords = this.data.payPasswords
    wx.request({//判断支付密码是否正确
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: res => {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag) {//正确
          console.log("密码正确");
          this.setData({
            payPasswords: '',
            showPayPwdInput: false
          })
          this.aa()//发起支付
        } else {//错误
          console.log("密码错误");
          this.setData({
            payPasswords: '',
            showPayPwdInput: false
          })
          wx.showToast({
            title: '密码错误!',
            icon: 'none',
            duration: 1000
          })
        }
      }
    }
    )
  },

  // 发起支付的请求
  aa() {
    var uid = this.data.uid
    if ((this.data.IsHavePayDeposit) - 0) {
      // 退还押金
      console.log("退还");
      wx.request({
        url: baseJs.api + baseJs.encryUrl('method=BusinessApplyOutDeposit&UID=' + uid),
        success: res => {
          var json = JSON.parse(baseJs.decryUrl(res.data))
          console.log(json);
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 900
          })
        },
        fail: err => {
          console.log(err);
        }
      })
    } else {
      console.log("支付");
      // 支付押金
      wx.request({
        url: baseJs.api + baseJs.encryUrl('method=BusinessPayDeposit&UID=' + uid + '&PayType=3'),
        success: res => {
          var json = JSON.parse(baseJs.decryUrl(res.data))
          console.log(json);
          wx.requestPayment({//调起微信支付
            nonceStr: json.ResultData.nonceStr,
            package: json.ResultData.package,
            paySign: json.ResultData.paySign,
            timeStamp: json.ResultData.timeStamp,
            signType: json.ResultData.signType,
            success: res => {
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 1500
              })
              wx.navigateBack()
            },
            fail: err => { //调起微信支付失败
              wx.showToast({
                title: err.err_desc,
                icon: 'none',
                duration: 1500
              })
            }
          })
        }
      })
    }
  }
})